$(function() {

	/**
	 * 全局变量
	 */
	var chart; // 饼图对象
	var pageNoOne;
	var pageSizeOne;
	var countOne;
	var pageNoTwo;
	var pageSizeTwo;
	var countTwo;
	var date = 30;

	chart = new Highcharts.Chart({
		// 常规图表选项设置
		chart : {
			renderTo : 'ampieDIV', // 在哪个区域呈现，对应HTML中的一个元素ID
			plotBackgroundColor : null, // 绘图区的背景颜色
			plotBorderWidth : null, // 绘图区边框宽度
			plotShadow : false
		// 绘图区是否显示阴影
		},
		// 图表的主标题
		title : {
			text : ''
		},
		// 当鼠标经过时的提示设置
		tooltip : {
			pointFormat : '{series.name}: <b>{point.y}</b>',
			percentageDecimals : 1
		},
		// 每种图表类型属性设置
		plotOptions : {
			// 饼状图
			pie : {
				allowPointSelect : true,
				cursor : 'pointer',
				dataLabels : {
					enabled : true,
					color : '#000000',
					connectorColor : '#000000',
					formatter : function() {
						// Highcharts.numberFormat(this.percentage,2)格式化数字，保留2位精度
						return '<b>' + this.point.name + '</b>: '
								+ Highcharts.numberFormat(this.percentage, 2)
								+ ' %';
					}
				},
				/* 是否显示图例 */
				showInLegend : true
			}
		},
		// 图表要展现的数据
		series : [ {
			type : 'pie',
			name : '订单数'
		} ]
	});
	// 隐藏logo
	$("text[text-anchor = 'end']").css("display", "none");
	
	// 页面加载时获取One表格数据
	$.getJSON("/jeesite/a/pro/clienteleReport/pieList", "day=" + date, function(data) {
		// 定义一个数组
		browsers = [];
		if (typeof(data.list) != "undefined" && data.list.length >= 1){
			$.each(data.list, function(i, info) {
				var newRow = "<tr><td>" + (i + 1) + "</td><td>" + info.name + "</td>"; 
				if(typeof(info.clienteleType) == "undefined" || typeof(info.clienteleType.name) == "undefined"){
					newRow += '<td></td>';
				}else{
					newRow += "<td>" + info.clienteleType.name + "</td>";
				}
				newRow +=	"<td>" + info.remarks + "</td><td>" + info.share + "</td></tr>";
				$("#customerList").append(newRow);
				//给数组赋值
				browsers.push([ info.name, info.share ]);
			});
			chart.series[0].setData(browsers);
			$("#previousPageLi").attr("class", "disabled");
			pageNoOne = data.pageNo;
			pageSizeOne = data.pageSize;
			countOne = data.count;
			$("#pageNoOne").val(pageNoOne);
			$("#pageSizeOne").val(pageSizeOne);
			$("#countOne").text(countOne);
			if (countOne / pageSizeOne <= 1) {
				$("#nextPageLi").attr("class", "disabled");
			}
		}
	});

	//One下一页
	$("#nextPage").click(function(){
		if(pageNoOne < countOne/pageSizeOne){
			//定义一个数组
			browsers = [];
			$("#customerList").html("");
			$.getJSON("/jeesite/a/pro/clienteleReport/pieList","pageNo="+(pageNoOne+1)+"&day="+date,function(data){
				$.each(data.list, function(i,info){
					var newRow = "<tr><td>" + (i + 1) + "</td><td>" + info.name + "</td>"; 
					if(typeof(info.clienteleType) == "undefined" || typeof(info.clienteleType.name) == "undefined"){
						newRow += '<td></td>';
					}else{
						newRow += "<td>" + info.clienteleType.name + "</td>";
					}
					newRow +=	"<td>" + info.remarks + "</td><td>" + info.share + "</td></tr>";
					$("#customerList").append(newRow);
					//给数组赋值
					browsers.push([ info.name, info.share ]);
				});
				chart.series[0].setData(browsers);
				pageNoOne = data.pageNo;
				pageSizeOne = data.pageSize;
				countOne = data.count;
				$("#pageNoOne").val(pageNoOne);
				$("#pageSizeOne").val(pageSizeOne);
				$("#countOne").text(countOne);
				$("#previousPageLi").attr("class","");
				if(pageNoOne >= countOne/pageSizeOne){
					$("#nextPageLi").attr("class","disabled");
				}
			});
		}
	});
	
	//One上一页
	$("#previousPage").click(function(){
		if(pageNoOne > 1){
			//定义一个数组
			browsers = [];
			$("#customerList").html("");
			$.getJSON("/jeesite/a/pro/clienteleReport/pieList","pageNo="+(pageNoOne-1)+"&day="+date,function(data){
				$.each(data.list, function(i,info){
					var newRow = "<tr><td>" + (i + 1) + "</td><td>" + info.name + "</td>"; 
					if(typeof(info.clienteleType) == "undefined" || typeof(info.clienteleType.name) == "undefined"){
						newRow += '<td></td>';
					}else{
						newRow += "<td>" + info.clienteleType.name + "</td>";
					}
					newRow +=	"<td>" + info.remarks + "</td><td>" + info.share + "</td></tr>";
					$("#customerList").append(newRow);
					//给数组赋值
					browsers.push([ info.name, info.share ]);
				});
				chart.series[0].setData(browsers);
				pageNoOne = data.pageNo;
				pageSizeOne = data.pageSize;
				countOne = data.count;
				$("#pageNoOne").val(pageNoOne);
				$("#pageSizeOne").val(pageSizeOne);
				$("#countOne").text(countOne);
				$("#nextPageLi").attr("class","");
				if(pageNoOne <= 1){
					$("#previousPageLi").attr("class","disabled");
				}
			});
		}
	});
	
	
	// 页面加载时获取Two表格数据
	$.getJSON("/jeesite/a/pro/clienteleReport/allList", "day=" + date, function(data) {
		if (typeof(data.list) != "undefined" && data.list.length >= 1){
			$.each(data.list, function(i, info) {
				var newRow = "<tr><td>" + info.name + 
					"</td><td>" + info.phone + 
					"</td><td>" + info.fax + 
					"</td><td>" + info.email + 
					"</td><td>" + info.remarks + 
					"</td><td>" + info.share + 
					"</td></tr>";
				$("#customerListTwo").append(newRow);
			});
			$("#previousPageLiTwo").attr("class", "disabled");
			pageNoTwo = data.pageNo;
			pageSizeTwo = data.pageSize;
			countTwo = data.count;
			$("#pageNoTwo").val(pageNoTwo);
			$("#pageSizeTwo").val(pageSizeTwo);
			$("#countTwo").text(countTwo);
			if (countTwo / pageSizeTwo <= 1) {
				$("#nextPageLiTwo").attr("class", "disabled");
			}
		}
	});

	//Two下一页
	$("#nextPageTwo").click(function(){
		if(pageNoTwo < countTwo/pageSizeTwo){
			$("#customerListTwo").html("");
			$.getJSON("/jeesite/a/pro/clienteleReport/allList","pageNo="+(pageNoTwo+1)+"&day="+date,function(data){
				$.each(data.list, function(i,info){
					 var newRow= "<tr><td>"+info.name+
					 "</td><td>"+info.phone+
					 "</td><td>"+info.fax+
					 "</td><td>"+info.email+
					 "</td><td>"+info.remarks+
					 "</td><td>"+info.share+
					 "</td></tr>";
					$("#customerListTwo").append(newRow);
				});
				pageNoTwo = data.pageNo;
				pageSizeTwo = data.pageSize;
				countTwo = data.count;
				$("#pageNoTwo").val(pageNoTwo);
				$("#pageSizeTwo").val(pageSizeTwo);
				$("#countTwo").text(countTwo);
				$("#previousPageLiTwo").attr("class","");
				if(pageNoTwo >= countTwo/pageSizeTwo){
					$("#nextPageLiTwo").attr("class","disabled");
				}
			});
		}
	});
	
	//Two上一页
	$("#previousPageTwo").click(function(){
		if(pageNoTwo > 1){
			$("#customerListTwo").html("");
			$.getJSON("/jeesite/a/pro/clienteleReport/allList","pageNo="+(pageNoTwo-1)+"&day="+date,function(data){
				$.each(data.list, function(i,info){
					 var newRow= "<tr><td>"+info.name+
					 "</td><td>"+info.phone+
					 "</td><td>"+info.fax+
					 "</td><td>"+info.email+
					 "</td><td>"+info.remarks+
					 "</td><td>"+info.share+
					 "</td></tr>";
					$("#customerListTwo").append(newRow);
				});
				pageNoTwo = data.pageNo;
				pageSizeTwo = data.pageSize;
				countTwo = data.count;
				$("#pageNoTwo").val(pageNoTwo);
				$("#pageSizeTwo").val(pageSizeTwo);
				$("#countTwo").text(countTwo);
				$("#nextPageLiTwo").attr("class","");
				if(pageNoTwo <= 1){
					$("#previousPageLiTwo").attr("class","disabled");
				}
			});
		}
	});	
	
	//历史
	$("#history").click(function(){pie(0)});
	//近60天
	$("#sixty").click(function(){pie(60)});
	//近30天
	$("#thirty").click(function(){pie(30)});
	//近10天
	$("#ten").click(function(){pie(10)});
	
	//按日期
	function pie(day){
		//获取One表格数据
		$.getJSON("/jeesite/a/pro/clienteleReport/pieList","day="+day,function(data){
			//定义一个数组
			browsers = [];
			$("#customerList").html("");
			$.each(data.list, function(i,info){
				var newRow = "<tr><td>" + (i + 1) + "</td><td>" + info.name + "</td>"; 
				if(typeof(info.clienteleType) == "undefined" || typeof(info.clienteleType.name) == "undefined"){
					newRow += '<td></td>';
				}else{
					newRow += "<td>" + info.clienteleType.name + "</td>";
				}
				newRow +=	"<td>" + info.remarks + "</td><td>" + info.share + "</td></tr>";
				$("#customerList").append(newRow);
				//给数组赋值
				browsers.push([ info.name, info.share ]);
			});
			chart.series[0].setData(browsers);
			$("#previousPageLi").attr("class","disabled");
			pageNoOne = data.pageNo;
			pageSizeOne = data.pageSize;
			countOne = data.count;
			$("#pageNoOne").val(pageNoOne);
			$("#pageSizeOne").val(pageSizeOne);
			$("#countOne").text(countOne);
			if(pageNoOne <= 1){
				$("#nextPageLi").attr("class","disabled");
			}
		});
		//获取Two表格数据
		$.getJSON("/jeesite/a/pro/clienteleReport/allList","day="+day,function(data){
			$("#customerListTwo").html("");
			$.each(data.list, function(i,info){
				 var newRow= "<tr><td>"+info.name+
				 "</td><td>"+info.phone+
				 "</td><td>"+info.fax+
				 "</td><td>"+info.email+
				 "</td><td>"+info.remarks+
				 "</td><td>"+info.share+
				 "</td></tr>";
				$("#customerListTwo").append(newRow);
			});
			$("#previousPageLiTwo").attr("class","disabled");
			pageNoTwo = data.pageNo;
			pageSizeTwo = data.pageSize;
			countTwo = data.count;
			$("#pageNoTwo").val(pageNoTwo);
			$("#pageSizeTwo").val(pageSizeTwo);
			$("#countTwo").text(countTwo);
			if(pageNoTwo <= 1){
				$("#nextPageLiTwo").attr("class","disabled");
			}
		});
		date = day;
	}	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
});